﻿package {
	import flash.display.*;
	import flash.net.*;
	import flash.events.*;
	import flash.filters.*;
	import flash.system.*;
	import flash.external.*;
	import flash.geom.*;
	import flash.utils.*;
	import flash.ui.*;
	
	public class Verify extends Sprite {
		private var tw:Number;
		private var th:Number;

		private var btimage:String;
		private var key:Number;
		private var callback:String;
		private var bgcolor:uint;
		private var onloaded:String;
		
		private var skin_up:Bitmap = new Bitmap();
		private var skin_over:Bitmap = new Bitmap();
		private var skin_down:Bitmap = new Bitmap();
		private var skin_disabled:Bitmap = new Bitmap();
		
		
		private var bt:SimpleButton = new SimpleButton();
		private var bg:Sprite = new Sprite();
		
		private var seed:int;
		private var vars:Object;

		public function Verify():void {
			Security.allowDomain("*");
			stage.align = "TL";
			stage.scaleMode = "noScale";
			stage.stageFocusRect = false;
			stage.addEventListener(Event.RESIZE, resizeHandler);
			stage.addEventListener(KeyboardEvent.KEY_UP, kbHandler);
			stage.showDefaultContextMenu = false;
			var m:ContextMenu = new ContextMenu();
			m.hideBuiltInItems();
			var ab:ContextMenuItem = new ContextMenuItem("cenfun@gmail.com");
			ab.addEventListener(ContextMenuEvent.MENU_ITEM_SELECT, abHandler);
			m.customItems = [ab];
			contextMenu = m;
			//
			vars = this.root.loaderInfo.parameters;
			bg.buttonMode = true;
			addChild(bg);
			addChild(bt);
			//
			seed = 1 + int(8 * Math.random());
			//
			key = parseInt(vars.key);
			callback = vars.callback;
			bgcolor = color(vars.bgcolor) || 0xffffff;
			btimage = vars.btimage;
			if (btimage) {
				load();
			}
			
			onloaded = vars.onloaded;
			if (onloaded && ExternalInterface.available) {
				try {
					ExternalInterface.call(onloaded);
				} catch (e:Error) {
				}
			}
			if (ExternalInterface.available) {
				ExternalInterface.addCallback("enabled", enabled);
				ExternalInterface.addCallback("clicked", clicked);
			}
		}
		
		private function abHandler(e:ContextMenuEvent):void {
			try {
				navigateToURL(new URLRequest("mailto:cenfun@gmail.com"));
			} catch (e:Error) {
			}
		}
		
		private function kbHandler(e:KeyboardEvent):void {
			var k:uint = e.keyCode;
			if(k == 13 ) {
				btClick();
			}
		}
		
		private function resizeHandler(e:Event = null):void {
			tw = stage.stageWidth;
			th = stage.stageHeight;
			back(bg, 0, 0, tw, th, 1, bgcolor);
			btMove();
		}
		
		private function load():void {
			var loader:Loader = new Loader();
			loader.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR, ioErrorHandler);
			loader.contentLoaderInfo.addEventListener(Event.COMPLETE, completeHandler);
			var request:URLRequest = new URLRequest(btimage);
            loader.load(request);
		}
		private function ioErrorHandler(e:IOErrorEvent):void {
        }
		private function completeHandler(e:Event):void {
            if (e.target.content.bitmapData) {
				var bmp:Array = sBD(e.target.content.bitmapData, 1, 4);
				skin_up = new Bitmap(bmp[0]);
				skin_over = new Bitmap(bmp[1]);
				skin_down = new Bitmap(bmp[2]);
				skin_disabled = new Bitmap(bmp[3]);
				bt.useHandCursor = true;
				bt.hitTestState = skin_up;
				bt.upState = skin_up;
				bt.overState = skin_over;
				bt.downState = skin_down;
				//
				bg.addEventListener(MouseEvent.CLICK, btMove);
				bt.addEventListener(MouseEvent.CLICK, btClick);
				//
				resizeHandler();
			}
        }
		


		private function btClick(e:MouseEvent = null):void {
			if (!bt.enabled) {
				return;
			}
			var m:Number = key << seed;
			var str:String = seed.toString() + m.toString();
			if (callback && ExternalInterface.available) {
				try {
					ExternalInterface.call(callback, str);
				} catch (e:Error) {
				}
			}
		}

		private function btMove(e:MouseEvent=null):void {
			if (!bt.enabled) {
				return;
			}
			if (tw > bt.width && th > bt.height ) {
				var tx:Number = int((tw - bt.width) * Math.random());
				var ty:Number = int((th - bt.height) * Math.random());
				bt.x = tx;
				bt.y = ty;
				if (bt.hitTestPoint(stage.mouseX, stage.mouseY)) {
					btMove();
				}
			} else {
				bt.x = (tw - bt.width) * 0.5;
				bt.y = (th - bt.height) * 0.5;
			}
		}
		
		
		private function enabled(v:Boolean):void {
			bt.enabled = v;
			if (v) {
				bt.upState = skin_up;
			} else {
				bt.upState = skin_disabled;
			}
		}
		
		private function clicked():void {
			btClick();
		}
		
		//====================================================================
		private function sBD(bmd:BitmapData, wn:int = 1, hn:int = 1):Array {
			//透明处理
			var list:Array = [];
			var aw:int = bmd.width;
			var ah:int = bmd.height;
			var ow:int = int(aw / wn);
			var oh:int = int(ah / hn);
			for (var i:int = 0; i < hn; i++) {
				var hd:BitmapData = new BitmapData(aw, oh, true, 0x00000000);
				hd.copyPixels(bmd, new Rectangle(0, oh * i, aw, oh), new Point(0, 0));
				for (var j:int = 0; j < wn; j++) {
					var wd:BitmapData = new BitmapData(ow, oh, true, 0x00000000);
					wd.copyPixels(hd, new Rectangle(ow * j, 0, ow, oh), new Point(0, 0));
					list.push(wd);
				}
				hd.dispose();
			}
			bmd.dispose();
			return list;
		}
		//绘制透明背景层
		private function back(sp:Sprite,_tx:Number,_ty:Number,_tw:Number,_th:Number,_ta:Number,_tc:Number):void {
			with (sp.graphics) {
				clear();
				beginFill(_tc,_ta);
				drawRect(_tx,_ty,_tw,_th);
				endFill();
			}
		}

		private function color(input:String):uint {
			input = String(input);
			//过滤#
			input = input.replace("#","");
			//返回16进制数字
			return parseInt(input,16);
		}

	}
}